package TorrentClient;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.*;
import java.util.Vector;
import java.io.*;

import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.TitledBorder;
import javax.swing.JTextArea;
import javax.swing.JProgressBar;


public class GUI {

	private JFrame frmTracker;
	private static JTextPane textFieldConsole;
	private final Action action = new SwingAction();
	private JTextField textFieldHashPath;
	public static JProgressBar progressBar;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUI window = new GUI();
					window.frmTracker.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public GUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmTracker = new JFrame();
		frmTracker.setTitle("Tracker 1.0");
		frmTracker.setResizable(false);
		frmTracker.setBounds(100, 100, 773, 659);
		frmTracker.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmTracker.getContentPane().setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 83, 751, 455);
		frmTracker.getContentPane().add(tabbedPane);
		
		
		JPanel tabConsole = new JPanel();
		tabbedPane.addTab("Console", null, tabConsole, null);
		tabbedPane.setEnabledAt(0, true);
		tabConsole.setLayout(null);
		
		JPanel panelConsole = new JPanel();
		panelConsole.setBounds(10, 11, 726, 405);
		tabConsole.add(panelConsole);
		panelConsole.setLayout(null);
		
		JScrollPane scrollPaneConsole = new JScrollPane();
		scrollPaneConsole.setBounds(10, 11, 706, 383);
		panelConsole.add(scrollPaneConsole);
		
		textFieldConsole = new JTextPane();
		textFieldConsole.setBackground(Color.BLACK);
		textFieldConsole.setForeground(Color.GREEN);
		textFieldConsole.setEditable(false);
		textFieldConsole.setFont(new Font("Arial", Font.PLAIN, 11));
		scrollPaneConsole.setViewportView(textFieldConsole);
		
		
		JLabel lblTracker = new JLabel("Client 1.0");
		lblTracker.setFont(new Font("Arial", Font.PLAIN, 11));
		lblTracker.setBounds(10, 28, 102, 14);
		frmTracker.getContentPane().add(lblTracker);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 761, 21);
		frmTracker.getContentPane().add(menuBar);
		
		JMenu fileMenu = new JMenu("File");
		menuBar.add(fileMenu);
		
		JMenu mnAbout = new JMenu("About");
		menuBar.add(mnAbout);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if(textFieldHashPath.getText()==null){
					GUI.updateConsole("You need to specify a hash file");
				}
				else{
					
					Client temp = new Client();
					   try {
						   BufferedReader br = new BufferedReader(new FileReader(textFieldHashPath.getText()));
					       StringBuilder sb = new StringBuilder();
					       String line = null;

					       while ((line = br.readLine()) != null) {
					         GUI.updateConsole(line.split(",")[2]);
					         String address = line.split(",")[2];
					         temp.synchClient(address, 4444, line);
					       }
					       br.close();
					   } catch (FileNotFoundException e){
						   e.printStackTrace();
					   }  catch (IOException e){
						   e.printStackTrace();
					   }
					      
				
	
				}
								   
			}
		});
		btnConnect.setBounds(84, 24, 89, 23);
		frmTracker.getContentPane().add(btnConnect);
		
		JLabel lblHashFile = new JLabel("Hash File:");
		lblHashFile.setBounds(207, 28, 68, 14);
		frmTracker.getContentPane().add(lblHashFile);
		
		textFieldHashPath = new JTextField();
		textFieldHashPath.setEditable(false);
		textFieldHashPath.setBounds(295, 25, 364, 20);
		frmTracker.getContentPane().add(textFieldHashPath);
		textFieldHashPath.setColumns(10);
		
		progressBar = new JProgressBar();
		progressBar.setStringPainted(true);
		progressBar.setBounds(10, 564, 751, 45);
		frmTracker.getContentPane().add(progressBar);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showOpenDialog(null);
				if(returnVal == JFileChooser.APPROVE_OPTION){
					File file = fc.getSelectedFile();
					textFieldHashPath.setText(file.getAbsolutePath());
				}
			}
		});
		btnSearch.setBounds(207, 62, 89, 23);
		frmTracker.getContentPane().add(btnSearch);
		
		   
		   ClientThread clientThread;
		try {
			clientThread = new ClientThread(50000);
			clientThread.start();
		} catch (BindException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void updateConsole(String message){
			textFieldConsole.setText(textFieldConsole.getText()+"\n"+message);
	}
	
	
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "SwingAction");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
		}
	}
	
	public static void rebuildProgressBar(int fileSize){
		progressBar = new JProgressBar(0,fileSize);
		progressBar.setValue(0);
		progressBar.repaint();
	}
	
	public static void updateProgressBar(int transferedBytes){
		
		progressBar.setValue(transferedBytes);
		progressBar.repaint();
	}
}
